/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package frontierserver;

/**
 *
 * @author ab61930
 */
import java.io.*;
import java.net.*;
import java.util.*;

class UDPServer {
    private Vector<clientList> IpVector = new Vector<clientList>();
    public DatagramSocket serverSocket;
    private int port;

	/*public static void main(String args[]) throws Exception
	{/*
		DatagramSocket serverSocket = new
					DatagramSocket(9876);
		byte[] receiveData = new byte[1024];
		byte[] sendData = new byte[1024];
		while(true)
		{
			DatagramPacket receivePacket =
			new DatagramPacket(receiveData, receiveData.length);
			serverSocket.receive(receivePacket);
			String sentence = new String(
				receivePacket.getData());
				InetAddress IPAddress =
				receivePacket.getAddress();
				int port = receivePacket.getPort();
				String capitalizedSentence = sentence.toUpperCase();
sendData = capitalizedSentence.getBytes();
			DatagramPacket sendPacket =
				new DatagramPacket(sendData, sendData.length, IPAddress, port);
				serverSocket.send(sendPacket);
		}

	}*/
        public UDPServer(int i)
        {
            setPort(i);
            try
            {
                setSocket(i);
            }
            catch(Exception obj)
            {
                System.out.print(obj);
            }

        }
        public void addClient(InetAddress IpAddy, int port)
        {
            clientList client = new clientList(IpAddy, port);
            if(!IpVector.contains(client))
            {
                IpVector.add(client);
            }
        }

        public void sendAll(byte[] sendData) throws Exception
        {
            Vector<DatagramPacket> packlist = new Vector<DatagramPacket>();
            for(int i = 0; i<IpVector.size(); i++)
            {
                System.out.print(IpVector.get(i).ipAddress);
                DatagramPacket sendPacket =
                        new DatagramPacket(sendData, sendData.length, IpVector.get(i).ipAddress, 35001);
                packlist.add(sendPacket);
            }
            for(int i = 0; i<packlist.size(); i++)
               serverSocket.send(packlist.get(i));
        }
        public void setSocket(int p) throws Exception
        {
            setPort(p);
            serverSocket = new DatagramSocket(p);
        }
        public void setPort(int pN)
        {
            port = pN;
        }
        public DatagramSocket getSocket()
        {
            return serverSocket;
        }

}
